<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
	String customer_id = request.getParameter("customer_id") == null ? "" : request.getParameter("customer_id");
	String customer = request.getParameter("customer") == null ? "" : request.getParameter("customer");
	String dept_id = request.getParameter("dept_id") == null ? "" : request.getParameter("dept_id");
	String dept_name = request.getParameter("dept_name") == null ? "" : request.getParameter("dept_name");
	String id = request.getParameter("id") == null ? "" : request.getParameter("id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建项目结算支付":"编辑项目结算支付" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var xm_id="<%=xm_id%>"; 
		var supply_id;
		var id="<%=id%>";
		var isInit=0;
		$(function(){
			$('#pay_value').numberbox('disable',true);
			if('<%=flag%>' == 1){
					jQuery('#ff').form({
						onLoadSuccess:function(){
						 $("#supply").combogrid("setValue",jQuery("#pay_to_id").val());
					 	 $("#supply").combogrid("setText",jQuery("#pay_to_name").val());
						}
					});
				 $('#ff').form('load','<%=contextPath %>/springMVC/xmFinance/viewXmPay?id=<%=id%>');
			}
			
			var url='<%=contextPath %>/springMVC/xmFinance/listXMSuppliers';
			$("#supply").combogrid({
				title:'选择客户',
				panelWidth:550,
				panelHeight:400,
				url:url,
				idField:"id",
				textField:"company",
				rownumbers:true,
				fitColumns:true,
				onBeforeLoad:function(param){
				param.xm_id=xm_id;
				},
				onChange:function(){
				$("#table").datagrid("reload");
				},
				columns: [[
					{field:'id',hidden:true},
					{field:'company',title:'供应商',width:100,align:'center'},
					{field:'tel',title:'电话',width:100,align:'center'}
				]],
				toolbar:'#tb'
			});
			
	       $('#table').datagrid({
	    	   	 url:'<%=contextPath %>/springMVC/xmFinance/listCGEndPayByXMIdAndsupplierId?xmPayId=<%=id%>',
				rownumbers:true,
				fitColumns:true,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'工单支付id',field:'bus_id',hidden:true}
				]],
				columns:[[
					{title:'工单id',field:'gd_id',hidden:true},
					{title:'工单编号',field:'gd_code',width:250,align:'center'},
					{title:'工单名称',field:'gd_name',width:150,align:'center'},
					{title:'应付结算款',field:'end_pay',width:200,align:'center'},
					{title:'已付结算款',field:'already_finance_out',width:200,align:'center',formatter:function(value,rowData,index){
						if(isNaN(rowData.already_finance_out)){
							rowData.already_finance_out=0;
						}
						
					return rowData.already_finance_out;
					}},						
					{title:'现付结算款',field:'now_pay',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2,
							myFlag:'1'
						}
					},formatter:function(value,rowData,index){
						if(isNaN(rowData.now_pay)){
							rowData.now_pay=rowData.end_pay-rowData.already_finance_out;
						}
						
					return rowData.now_pay;
					}},		
					{title:'备注',field:'remark',width:200,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(param){
					param.xm_id=xm_id;
					param.supplier_id=$("#supply").combogrid("getValue");
					param.xmPayId=id;
					$(this).datagrid('rejectChanges');
					
				},
				onAfterEdit:function(rowIndex, rowData, changes){
					addPayValue();
				},
				onLoadSuccess:function(rtJson){
					addPayValue();
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'now_pay'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						addPayValue();	
	
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = $('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = $('#table').datagrid('getRowIndex', row);
								$('#table').datagrid('deleteRow', index);
							}
						}
						var data = $('#table').datagrid('getData');
						$('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
			
		});
		function addPayValue(){
		var jsondata=$("#table").datagrid("getData");
			var supplyPay=0;
			for(var i=0;i<jsondata.total;i++){
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				if(jsondata.rows[i].now_pay == null || jsondata.rows[i].now_pay == "" || jsondata.rows[i].now_pay <= 0){
						$.messager.alert('提示','第'+index+'行支付金额有误！');
						return ;
				}else if(parseFloat(jsondata.rows[i].now_pay)>parseFloat(jsondata.rows[i].finance_out)){
						$.messager.alert('提示','第'+index+'行支付金额不能大于应付金额！');
						return ;
				}
				else{
					supplyPay=parseFloat(supplyPay)+parseFloat(jsondata.rows[i].now_pay);		
				}
			}
			jQuery("#pay_value").numberbox('setValue', supplyPay);						
		}
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		
		function addXMPaymentSupply(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			addPayValue();
			//自动提交数据
			var xm_id =jQuery("#xm_id").val();	
			var xm_name=jQuery("#xm_name").val();	
			var xm_code=jQuery("#xm_code").val();
			var pay_value=jQuery("#pay_value").val();
			var remark=encodeURIComponent(jQuery('#remark').val());
			if(!checkNullOREmpty(xm_id,'项目为必填项')){return;}
			var pay_to_id = $("#supply").combogrid("getValue");
			var pay_to_name =encodeURIComponent( $("#supply").combogrid("getText"));
			if(!checkNullOREmpty(pay_to_id,'供应商为必填项')){return;}	
			if(parseFloat(pay_value)==0)
			{
				$.messager.alert('提示','支付总额不能为0！');
				return;
			}
			
			var gds="";
			var jsondata=$("#table").datagrid("getData");
			for(var i=0;i<jsondata.total;i++){
				if(gds!=""){
					gds+=";";
				}
				gds+=jsonToStr(jsondata.rows[i]);
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				if(jsondata.rows[i].now_pay == null || jsondata.rows[i].now_pay == "" || jsondata.rows[i].now_pay <= 0){
					$.messager.alert('提示','第'+index+'行支付金额有误！');
					return ;
				}else if(parseFloat(jsondata.rows[i].now_pay)>parseFloat(jsondata.rows[i].finance_out)){
					$.messager.alert('提示','第'+index+'行支付金额不能大于应付金额！');
					return ;
				}
				
			}
			var param = "remark="+remark
			+"&pay_value="+pay_value+"&pay_to_id="+pay_to_id+"&pay_to_name="+pay_to_name
			+"&xm_id="+xm_id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&gds="+gds;
			$(obj).hide();
			$.ajax({
				type:'POST',
				<%=isAdd?"url:'"+contextPath+"/springMVC/xmFinance/addXMPayEndSupply',":"url:'"+contextPath+"/springMVC/xmFinance/updateXMEndPaySupply?id="+id+"',"%>
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						eval('window.opener.<%=callback%>();');
						window.close();
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					$(obj).show();
				}
			});
		}


	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
		<input  type="hidden" id="pay_to_id" name="pay_to_id" >
		<input  type="hidden" id="pay_to_name" name="pay_to_name" >
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly value="<%=xm_code%>" placeholder="项目验收后停止进度付款">
						<input type="hidden" id="xm_id" name="xm_id" readOnly value="<%=xm_id%>">
					</td>
	
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly value="<%=xm_name%>" placeholder="项目验收后停止进度付款">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>项目部门：</td>
					<td class="td2" nowrap>
						<input  id="dept_name" name="dept_name" readOnly value="<%=dept_name%>">
						<input  type="hidden" id="dept_id" name="dept_id" readOnly value="<%=dept_id%>">
					</td>
					<td align="right" class="td1" nowrap>经办人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>对应客户：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="customer" name="customer" readOnly value="<%=customer%>">
						<input  type="hidden" id="customer_id" name="customer_id" readOnly value="<%=customer_id%>">
					</td>
				</tr>
				<tr>
			      <td align="right" class="td1" nowrap>选择供应商：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="supply" name="supply" 	<%if(flag==1){%> readonly <%}%> >
					</td>	
				</tr>
			
				<tr>	
			        <td align="right" class="td1" nowrap>付款总额：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="pay_value" name="pay_value" class="easyui-numberbox" data-options="min:0,precision:2" value="0" >(自动计算)
					</td>
	
			       
				</tr>				
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
						   <%if(!"1".equals(isShow)){ %>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addXMPaymentSupply(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
							<%} %>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
	</body>
</html>